﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace exSplitter
{
    /// <summary>
    /// DataGrid.xaml 的交互逻辑
    /// </summary>
    public partial class DataGrid : Window
    {
        public DataGrid()
        {
            InitializeComponent();
            //var cars = new List<Car>()
            //{
            //    new  Car(){CarName = "1"},
            //    new  Car(){ CarName ="2" }
            //};
            //var ocars = new ObservableCollection<Car>();
            //foreach (var c in cars)
            //{
            //    ocars.Add(c);
            //}

            //foreach (var c in cars)
            //{
            //    ocars.Add(c);
            //}

            //var c0 = cars[0];
            //ocars.Remove(c0);
            var vm = new VM();
            this.DataContext = vm;
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            this.DataContext = null;
            this.Resources["NoData"] = "No data...";
        }

        private void Change_OnClick(object sender, RoutedEventArgs e)
        {
            this.Resources["NoData"] = "暂无数据";
        }

        private void AddItem_OnClick(object sender, RoutedEventArgs e)
        {
            var vm = this.DataContext as VM;
            Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                foreach (var car in Enumerable.Range(0, 5000).Select(i => new Car
                {
                    CarName = "CarName" + i,
                    CarType = "CarType" + i,
                    CarMade = "CarMade" + i
                }))
                {
                    vm.CarList.Add(car);
                }
            }), DispatcherPriority.Background);
        }
    }

    public class VM
    {
        public string Name { get; set; } = "aaa&bbb";

        public ObservableCollection<Car> CarList { get; set; }

        public VM()
        {
            CarList = new ObservableCollection<Car>(Enumerable.Range(0, 5).Select(i => new Car
            {
                CarName = "CarName" + i,
                CarType = "CarType" + i,
                CarMade = "CarMade" + i
            }));
        }
    }

    public class Car
    {
        public string CarName { get; set; }
        public string CarType { get; set; }
        public string CarMade { get; set; }
    }
}
